package com.cskaoyan.javase.homework.primary._6object;

/**
 * @description: equals方法练习
 * @author: wuguidong@cskaoyan.onaliyun.com
 **/

public class EqualsDemo {
}

class Animal{
    private int age;
    private String name;
    private double price;

    //重写equals方法的Select all non-null fields选项，选中后默认该引用类型成员变量不会是null，比如下面的name属性，就去掉了判null的操作
    /*@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Animal animal = (Animal) o;

        if (age != animal.age) return false;
        if (Double.compare(animal.price, price) != 0) return false;
        return name.equals(animal.name);
    }*/

    //如果不勾选该选项，默认该引用类型成员变量可能是null，会判null
    /*@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Animal animal = (Animal) o;

        if (age != animal.age) return false;
        if (Double.compare(animal.price, price) != 0) return false;
        return name != null ? name.equals(animal.name) : animal.name == null;
        //Objects.equals()
    }*/

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Animal animal = (Animal) o;

        if (age != animal.age) return false;
        if (Double.compare(animal.price, price) != 0) return false;
        return name != null ? name.equals(animal.name) : animal.name == null;
        //Objects.equals(, )
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = age;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        temp = Double.doubleToLongBits(price);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }
}
